﻿using AlarmPlatformInterface.DMs;
using Entity.Models.Base;
using Entity.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Utility;
using AlarmPlatformInterface.Comm;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AlarmPlatformInterface.Controllers.AddControllers
{
    [Route("api/AlarmPlatForm/[controller]")]
    [ApiController]
    public class AddOfficerBindController : ControllerBase
    {
        // POST api/<AddOfficerBindController>
        [HttpPost]
        public ContentResult Post([FromBody] ReqBaseModel req)
        {
            return AddOfficerBind(req);
        }

        private ContentResult AddOfficerBind(ReqBaseModel value)
        {
            ResBaseModel r = new ResBaseModel();
            try
            {
                string newToken = "";
                OperatorInfo uinfo = new OperatorInfo();
                if (Global.ValidateToken(ref value, ref newToken, ref uinfo))
                {
                    AlarmGroupOfficerBind data = value.Parameters.JSONStringToObject<AlarmGroupOfficerBind>();
                    using (Entity.DBContext context = Entity.BaseContext.CreateContext(Global.Configuration["ConnectionStrings:DDb"]))
                    {
                        if (data.BindId > 0)
                        {
                            //var v = context.officerBinds.Where(p => p.DeviceId == data.DeviceId && p.AlarmGroupId != data.AlarmGroupId && p.BindId != data.BindId).FirstOrDefault();
                            //if (v != null)
                            //{
                            //    r.IsSuccess = false;
                            //    r.Token = newToken;
                            //    r.Result = "同个设备不能绑定到多个报警组里！";
                            //    r.FailType = eFailType.Known.ToString();
                            //    string vs = JsonConvert.SerializeObject(r);
                            //    return Content(vs);
                            //}
                            context.officerBinds.Update(data);
                        }
                        else
                        {
                            //var v = context.officerBinds.Where(p => p.DeviceId == data.DeviceId && p.AlarmGroupId != data.AlarmGroupId).FirstOrDefault();
                            //if (v != null)
                            //{
                            //    r.IsSuccess = false;
                            //    r.Token = newToken;
                            //    r.Result = "同个设备不能绑定到多个报警组里！";
                            //    r.FailType = eFailType.Known.ToString();
                            //    string vs = JsonConvert.SerializeObject(r);
                            //    return Content(vs);
                            //}
                            context.officerBinds.Add(data);
                        }
                        int c = context.SaveChanges();
                        if (c > 0)
                        {
                            r.IsSuccess = true;
                            r.Token = newToken;
                            r.Result = "编辑成功！";
                        }
                        else
                        {
                            r.Token = newToken;
                            r.FailType = eFailType.ParamsError.ToString();
                            r.Result = "编辑失败，请重试！";
                        }
                    }
                }
                else
                {
                    r.FailType = eFailType.LoginAgain.ToString();
                    r.Result = "登录过期，请重新登录！";
                    return Content(JsonConvert.SerializeObject(r));
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteExceptionFile("Add Officer Bind Info Controller : " + ex.Message + "\r\n" + ex.StackTrace);
                r.FailType = eFailType.ServerError.ToString();
                r.Result = "服务器错误！";
            }
            return Content(JsonConvert.SerializeObject(r));
        }
    }
}
